package com.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.interceptor.JwtInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * ClassName: MvcConfig
 * Description:
 *
 * @author L_Z_R
 * @create 2025/05/12 19:26
 * @version 1.0
 */
// 标识为配置类
@Configuration
// 启用MVC注解
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    private static final Logger logger = LoggerFactory.getLogger(MvcConfig.class);

    /**
     * @author: kx
     * @date: 2025/5/18 18:37
     * @param configurer : 配置对象
     * @description: 配置默认Servlet处理
    */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        // 启用默认servlet
        configurer.enable();
    }

    /**
     * 全局跨域设置
     * @param registry 跨域配置注册器
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //之后新的允许跨域的方法
                .allowedOriginPatterns("http://127.0.0.1:5500",
                        "http://10.11.200.122:8080",
                        "http://localhost:8080",  // 添加前端开发地址
                        "http://localhost:3000"   // 代理服务器地址)
                //.allowedOrigins("http://127.0.0.1:5500"
                                )
                .allowedMethods("*")
                .allowedHeaders("*")
                .exposedHeaders("*")
                .allowCredentials(true);
        logger.info("全局跨域配置已生效");
    }


    //装配拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //配置拦截器
        registry.addInterceptor(new JwtInterceptor())
                .addPathPatterns("/auth/**"); //拦截请求
                //.excludePathPatterns("/auth.html"); //放行这些请求
    }

    //装配本地静态图片资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**")
                .addResourceLocations("File:/D:/project/fish/Fishing_Test/","File:/D:/project/fish/fish/src/main/webapp"
                        ,"File:/D:/project/fish/fish/target/Fishing_forums-1.0-SNAPSHOT");
    }
}
